home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-18 | 12.1 KB | 482 lines |
- /*
- * @(#)ISAPIRequest.java 1.13 97/06/16
- *
- * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
- *
- * This software is the confidential and proprietary information of Sun
- * Microsystems, Inc. ("Confidential Information"). You shall not
- * disclose such Confidential Information and shall use it only in
- * accordance with the terms of the license agreement you entered into
- * with Sun.
- *
- * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
- * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
- * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
- * THIS SOFTWARE OR ITS DERIVATIVES.
- *
- * CopyrightVersion 1.0
- */
-
- package sun.servlet.isapi;
-
- import java.io.IOException;
- import java.util.Date;
- import java.util.Dictionary;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import javax.servlet.ServletInputStream;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpUtils;
- import java.util.StringTokenizer;
-
- /**
- * This class represents a servlet request in the ISAPI servlet runner.
- *
- * @version 1.13, 06/16/97
- * @author David Connelly
- * @author Jongyoon Lee
- */
- class ISAPIRequest implements HttpServletRequest {
- /*
- * The ISAPI connection for this request.
- */
- private ISAPIConnection conn;
-
- /*
- * The input stream for this request.
- */
- private ISAPIInputStream in = new ISAPIInputStream();
-
- /*
- * The parameters for this request.
- */
- private Hashtable params;
-
- /*
- * The headers for this request.
- */
- private Hashtable headers;
-
- /*
- * The REQUEST_URI for this request.
- */
- private String requestURI;
-
- /*
- * The PATH_INFO for this request.
- */
- private String pathInfo;
-
- /*
- * The SERVLET_PATH for this request.
- */
- private String servletPath;
-
- /*
- * The PATH_TRANSLATED for this request.
- */
- private String pathTranslated;
-
- /*
- * The virtual path to the servlets.
- */
- private final String SERVLETPATH = "/servlet";
-
- /*
- * Possible request header field names.
- */
- private static String[] HEADERS = {
- /* Standard HTTP 1.0 and 1.1 headers */
- "Accept", "Accept-Charset", "Accept-Encoding",
- "Accept-Language", "Accept-Ranges", "Age",
- "Allow", "Authorization", "Cache-Control",
- "Connection", "Content-Base", "Content-Encoding",
- "Content-Language", "Content-Length", "Content-Location",
- "Content-MD5", "Content-Range", "Content-Type",
- "Date", "ETag", "Expires",
- "From", "Host", "If-Modified-Since",
- "If-Match", "If-None-Match", "If-Range",
- "If-Unmodified-Since", "Last-Modified", "Location",
- "Max-Forwards", "Pragma", "Proxy-Authenticate",
- "Proxy-Authorization", "Public", "Range",
- "Referer", "Retry-After", "Server",
- "Transfer-Encoding", "Upgrade", "User-Agent",
- "Vary", "Via", "Warning",
- "WWW-Authenticate",
- /* Popular extension headers */
- "Alternates", "Content-Version", "Cookie",
- "Derived-From", "Keep-Alive", "Link",
- "MIME-Version", "URI", "Title"
- };
-
- ISAPIRequest() {
- conn = null;
- }
-
- ISAPIRequest(ISAPIConnection conn) {
- this.conn = conn;
- in.init(conn);
- }
-
- public void reset() {
- in.resets();
- requestURI = null;
- pathInfo = null;
- servletPath = null;
- pathTranslated = null;
- }
-
- public void init(ISAPIConnection conn) {
- this.conn = conn;
- in.init(conn);
- }
-
- /**
- * Returns the size of the request entity data, or -1 if not known.
- * Same as the CGI variable CONTENT_LENGTH.
- */
- public int getContentLength() {
- String str = conn.getServerVariable("CONTENT_LENGTH");
- int len;
- try {
- len = Integer.parseInt(str);
- } catch (Exception e) {
- return 0;
- }
- return len;
- }
-
- /**
- * Returns the Internet Media Type of the request entity data, or
- * null if not known. Same as the CGI variable CONTENT_TYPE.
- */
- public String getContentType() {
- return conn.getContentType();
- }
-
- /**
- * Returns the protocol and version of the request as a string of the
- * form <protocol>/<major version>.<minor version>.
- * Same as the CGI variable SERVER_PROTOCOL.
- */
- public String getProtocol() {
- return conn.getServerVariable("SERVER_PROTOCOL");
- }
-
- /**
- * Returns the scheme of the request -- always "http" for now.
- */
- public String getScheme() {
- return "http"; // XXX might be "https" too!
- }
-
- /**
- * Returns the host name of the server that received the request.
- * Same as the CGI variable SERVER_NAME.
- */
- public String getServerName() {
- return conn.getServerVariable("SERVER_NAME");
- }
-
- /**
- * Returns the port number on which this request was received.
- * Same as the CGI variable SERVER_PORT.
- */
- public int getServerPort() {
- try {
- return Integer.parseInt(conn.getServerVariable("SERVER_PORT"));
- } catch (NumberFormatException e) {
- return -1;
- }
- }
-
- /**
- * Returns the IP address of the agent that sent the request.
- * Same as the CGI variable REMOTE_ADDR.
- */
- public String getRemoteAddr() {
- return conn.getServerVariable("REMOTE_ADDR");
- }
-
- /**
- * Returns the fully qualified host name of the agent that sent the
- * request. Same as the CGI variable REMOTE_HOST.
- */
- public String getRemoteHost() {
- return conn.getServerVariable("REMOTE_HOST");
- }
-
- /**
- * Applies alias rules to the specified virtual path and returns the
- * corresponding real path. getRealPath("/") is the document root.
- * It returns null if the translation cannot be performed.
- * @param path the path to be translated
- */
- public String getRealPath(String path) {
- return conn.getRealPath(path);
- }
-
- /**
- * Returns an input stream for reading the request body.
- */
- public ServletInputStream getInputStream() {
- return in;
- }
-
- /**
- * Returns the value of the specified query parameter, or null if none.
- * @param name the query parameter name
- */
- public String getParameter(String name) {
- if (params == null) {
- params = getParameters();
- }
-
- String vals[] = (String []) params.get(name);
-
- if (vals == null)
- return null;
-
- String hackVal = vals[0];
- for (int i = 1; i < vals.length; i++)
- hackVal = hackVal + "," + vals[i];
-
- return hackVal;
- }
-
- /**
- * Returns an array of values for the specified query parameter.
- */
- public String [] getParameterValues(String name) {
- if (params == null)
- params = getParameters();
-
- return (String []) params.get(name);
- }
-
- /**
- * Returns a Dictionary of all the query parameters for this request.
- */
- public Enumeration getParameterNames() {
- if (params == null) {
- params = getParameters();
- }
- return params.keys();
- }
-
- /**
- * Returns an attribute of the request given the specified key name.
- * This allows access to request information not already provided by
- * the other methods in this interface. Key names beginning with
- * 'COM.sun.*' are reserved.
- * @param name the attribute name
- * @return the value of the attribute, or null if not defined
- */
- public Object getAttribute(String name) {
- return null;
- }
-
- /*
- * Return a hashtable of parameters for this request.
- */
- private Hashtable getParameters() {
- Hashtable h = null;
-
- String method = getMethod();
- String queryString = null;
- if (method.equals("GET")) {
- queryString = getQueryString();
- } else if (method.equals("POST")) {
- queryString = getPostedData();
- }
-
- try {
- h = HttpUtils.parseQueryString(queryString);
- } catch (IllegalArgumentException e) {
- h = null;
- }
- return h;
- }
-
- /**
- * Returns the method with which the request was made. The returned
- * value can be "GET", "HEAD", "POST", or an extension method. Same
- * as the CGI variable REQUEST_METHOD.
- */
- public String getMethod() {
- return conn.getMethod();
- }
-
- /**
- * Returns the request URI.
- */
- public String getRequestURI() {
- if (requestURI == null) {
- parsePath();
- }
-
- return requestURI;
- }
-
- /**
- * Returns the part of the request URI that refers to the servlet
- * being invoked. Analogous to the CGI variable SCRIPT_NAME.
- */
- public String getServletPath() {
- if (servletPath == null) {
- parsePath();
- }
-
- return servletPath;
- }
-
- /**
- * Returns optional extra path information following the servlet path,
- * but immediately preceding the query string. Returns null if not
- * specified. Same as the CGI variable PATH_INFO.
- */
- public String getPathInfo() {
- if (pathInfo == null) {
- parsePath();
- }
-
- return pathInfo;
- }
-
- /**
- * Returns extra path information translated to a real path. Returns
- * null if no extra path information specified. Same as the CGI variable
- * PATH_TRANSLATED.
- */
- public String getPathTranslated() {
- if (pathTranslated == null) {
- parsePath();
- }
-
- return pathTranslated;
- }
-
- /**
- * Returns the query string part of the servlet URI, or null if none.
- * Same as the CGI variable QUERY_STRING.
- */
- public String getQueryString() {
- return conn.getQueryString();
- }
-
- /**
- * Returns the name of the user making this request, or null if not
- * known. Same as the CGI variable REMOTE_USER.
- */
- public String getRemoteUser() {
- return conn.getServerVariable("REMOTE_USER");
- }
-
- /**
- * Returns the authentication scheme of the request, or null if none.
- * Same as the CGI variable AUTH_TYPE.
- */
- public String getAuthType() {
- return conn.getServerVariable("AUTH_TYPE");
- }
-
- /**
- * Returns the value of a header field, or null if not found. The
- * case of the header field name is ignored.
- * @param name the header field name
- */
- public String getHeader(String name) {
- return conn.getServerVariable("HTTP_" + name.replace('-', '_'));
- }
-
- /**
- * Returns the value of an integer header field or -1 if not found.
- * The case of the header name is ignored.
- * @param name the header field name
- */
- public int getIntHeader(String name) {
- String value = getHeader(name);
- if (value != null) {
- try {
- return Integer.parseInt(value);
- } catch (NumberFormatException e) {
- return -1;
- }
- }
- return -1;
- }
-
- /**
- * Returns the value of a date header field or -1 if not found. The
- * case of the header field name ignored.
- * @param name the header field name
- */
- public long getDateHeader(String name) {
- String value = getHeader(name);
- if (value != null) {
- try {
- return new Date(value).getTime();
- } catch (IllegalArgumentException e) {
- return -1;
- }
- }
- return -1;
- }
-
- /**
- * Returns a Dictionary of header field names and values for this
- * request. The header field names are guaranteed to be all lower case.
- */
- public Enumeration getHeaderNames() {
- if (headers == null) {
- headers = new Hashtable(HEADERS.length);
- for (int i = 0; i < HEADERS.length; i++) {
- String name = HEADERS[i];
- String value = getHeader(name);
- if (value != null && !value.equals("")) {
- headers.put(name, value);
- }
- }
- }
- return headers.keys();
- }
-
- /*
- * Parses server variables and modifies it for the servlet
- */
- private void parsePath() {
- int index;
- String path = conn.getPathInfo();
- requestURI = SERVLETPATH + path;
- String queryString = conn.getQueryString();
- if (queryString != null && queryString.length() > 0) {
- requestURI = requestURI + "?" + queryString;
- }
- if (path != null) {
- index = path.indexOf('/', 1);
- if (index != -1) {
- servletPath = SERVLETPATH + path.substring(0, index);
- pathInfo = path.substring(index);
- } else {
- servletPath = SERVLETPATH + path.substring(0);
- pathInfo = null;
- }
- }
- if (pathInfo != null) {
- pathTranslated = conn.getRealPath(pathInfo);
- }
- }
-
- /*
- * Fetches the posted data
- */
- private String getPostedData() {
- int len = getContentLength();
- byte[] buf = new byte[len];
- conn.getData(0, buf, 0, len);
- return new String(buf);
- }
- }
-